//
//  UIStandardBirthdayView.m
//  vhECGTools
//
//  Created by jia yu on 2022/2/14.
//

#import "UIStandardBirthdayView.h"
#import <vhECGTools/UIStandardSelectButton.h>

@interface UIStandardBirthdayView(){
    float scale;
    UIStandardSelectButton *yearSelectButton;
    UIStandardSelectButton *monthSelectButton;
    UIStandardSelectButton *daySelectButton;
    NSMutableArray *yearsArray;
    NSMutableArray *monthAvailableArray;
    NSMutableArray *bigMonthDaysArray;      //31
    NSMutableArray *smallMonthDaysArray;    //30
    NSMutableArray *leapMonthDaysArray;     //29
    NSMutableArray *ssMonthDaysArray;       //28
    
}
@end

@implementation UIStandardBirthdayView
@synthesize enabled;
@synthesize birthday;
@synthesize handler;

-(void)setEnabled:(BOOL)_enabled{
    enabled = _enabled;
    yearSelectButton.enabled = enabled;
    monthSelectButton.enabled = enabled;
    daySelectButton.enabled = enabled;
}

-(void)setBirthday:(NSDate *)_birthday{
    birthday = _birthday;
    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    [df setLocale:[NSLocale currentLocale]];
    [df setTimeZone:[NSTimeZone systemTimeZone]];
    
    [df setDateFormat:@"yyyy"];
    float y = [[df stringFromDate:birthday] floatValue];
    [df setDateFormat:@"MM"];
    float m = [[df stringFromDate:birthday] floatValue];
    [df setDateFormat:@"dd"];
    float d = [[df stringFromDate:birthday] floatValue];
    
    for (NSItemObject *item in yearsArray){
        if (item.inner == y){
            [yearSelectButton setTitle:item.str forState:UIControlStateNormal];
            yearSelectButton.inner = item.inner;
            break;
        }
    }
    
    for (NSItemObject *item in monthAvailableArray){
        if (item.inner == m){
            [monthSelectButton setTitle:item.str forState:UIControlStateNormal];
            monthSelectButton.inner = item.inner;
            break;
        }
    }
    
    for (NSItemObject *item in bigMonthDaysArray){
        if (item.inner == d){
            [daySelectButton setTitle:item.str forState:UIControlStateNormal];
            daySelectButton.inner = item.inner;
            break;
        }
    }
    return;
}

-(NSDate *)birthday{
    NSString *str = [NSString stringWithFormat:@"%04.0f-%02.0f-%02.0f", yearSelectButton.inner, monthSelectButton.inner, daySelectButton.inner];
    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    [df setLocale:[NSLocale currentLocale]];
    [df setTimeZone:[NSTimeZone systemTimeZone]];
    [df setDateFormat:@"yyyy-MM-dd"];
    NSDate *date = [df dateFromString:str];
    return date;
}

-(id)initWithFrame:(CGRect)frame sacle:(float)_scale{
    self = [super initWithFrame:frame];
    if (self){
        scale = _scale;
        float space = 20.f * scale;
        float width = (frame.size.width - 2.f * space ) / 3.f;
        UIStandardBirthdayView * __strong strongSelf = self;
        yearsArray = [[NSMutableArray alloc] init];
        int year = 2022;
        for (int i = year-100;i<year-2;i++){
            NSItemObject *item = [[NSItemObject alloc] init];
            item.str = [NSString stringWithFormat:@"%d", i];
            item.inner = i;
            [yearsArray addObject:item];
        }

        ssMonthDaysArray = [[NSMutableArray alloc] init];
        leapMonthDaysArray = [[NSMutableArray alloc] init];
        smallMonthDaysArray = [[NSMutableArray alloc] init];
        bigMonthDaysArray = [[NSMutableArray alloc] init];
        for (int i=1;i<=28;i++){
            NSItemObject *item = [[NSItemObject alloc] init];
            item.str = [NSString stringWithFormat:@"%d", i];
            item.inner = i;
            [ssMonthDaysArray addObject:item];
            [leapMonthDaysArray addObject:item];
            [smallMonthDaysArray addObject:item];
            [bigMonthDaysArray addObject:item];
        }
        NSItemObject *item29 = [[NSItemObject alloc] init];
        item29.str = @"29";
        item29.inner = 29;
        [leapMonthDaysArray addObject:item29];
        [smallMonthDaysArray addObject:item29];
        [bigMonthDaysArray addObject:item29];
        NSItemObject *item30 = [[NSItemObject alloc] init];
        item30.str = @"30";
        item30.inner = 30;
        [smallMonthDaysArray addObject:item30];
        [bigMonthDaysArray addObject:item30];
        NSItemObject *item31 = [[NSItemObject alloc] init];
        item31.str = @"31";
        item31.inner = 31;
        [bigMonthDaysArray addObject:item31];

        yearSelectButton = [[UIStandardSelectButton alloc] initWithFrame:CGRectMake(0.f, 0.f, width, frame.size.height) scale:scale];
        yearSelectButton.changedHandler = ^(NSString * _Nonnull targetString) {
            [strongSelf yearChanged];
            if (strongSelf->handler){
                strongSelf->handler(strongSelf);
            }
            return;
        };
        yearSelectButton.availableItems = yearsArray;
        [yearSelectButton setTitle:@"--" forState:UIControlStateNormal];
        NSDateComponents *c = [NSCalendar.currentCalendar componentsInTimeZone:NSTimeZone.systemTimeZone fromDate:NSDate.date];
        yearSelectButton.defaultSelectedString = [NSString stringWithFormat:@"%d", (int)(c.year-30)];
        [self addSubview:yearSelectButton];
        
        monthSelectButton = [[UIStandardSelectButton alloc] initWithFrame:CGRectMake(width + space, 0.f, width, frame.size.height) scale:scale];
        monthSelectButton.changedHandler = ^(NSString * _Nonnull targetString) {
            [strongSelf monthChanged];
            if (strongSelf->handler){
                strongSelf->handler(strongSelf);
            }
            return;
        };
        monthAvailableArray = [[NSMutableArray alloc] init];
        for (int i=1;i<=12;i++){
            NSItemObject *item = [[NSItemObject alloc] init];
            item.str = [NSString stringWithFormat:@"%d", i];
            item.inner = i;
            [monthAvailableArray addObject:item];
        }
        monthSelectButton.defaultSelectedString = [NSString stringWithFormat:@"%d", (int)c.month];
        monthSelectButton.availableItems = monthAvailableArray;
        [monthSelectButton setTitle:@"--" forState:UIControlStateNormal];
        [self addSubview:monthSelectButton];
        
        daySelectButton =  [[UIStandardSelectButton alloc] initWithFrame:CGRectMake(2.f * width + 2.f * space, 0.f, width, frame.size.height) scale:scale];
        daySelectButton.changedHandler = ^(NSString * _Nonnull targetString) {
            if (strongSelf->handler){
                strongSelf->handler(strongSelf);
            }
            return;
        };
        daySelectButton.defaultSelectedString = [NSString stringWithFormat:@"%d", (int)c.day];
        daySelectButton.availableItems = bigMonthDaysArray;
        [daySelectButton setTitle:@"--" forState:UIControlStateNormal];
        [self addSubview:daySelectButton];
    }
    return self;
}

-(id)initWithFrame:(CGRect)frame{
    NSLog(@"Please -(id)initWithFrame:(CGRect)frame sacle:(float)scale;");
    return nil;
}

-(BOOL)leadYear:(NSInteger)year{
    if (0 == year % 400){
        return YES;
    }
    if ( (0 == year%4) && (0 != year%100) ){
        return YES;
    }
    return NO;
}

-(void)yearChanged{
    if ( 2 == (NSInteger)monthSelectButton.inner){
        [self monthChanged];
    }
}

-(void)monthChanged{
    NSInteger month = (NSInteger)monthSelectButton.inner;
    switch (month) {
        case 1:
            daySelectButton.availableItems = bigMonthDaysArray;
            break;
        case 2:
            if ([self leadYear:(NSInteger)yearSelectButton.inner]){
                daySelectButton.availableItems = leapMonthDaysArray;
            }else{
                daySelectButton.availableItems = ssMonthDaysArray;
            }
            break;
        case 3:
            daySelectButton.availableItems = bigMonthDaysArray;
            break;
        case 4:
            daySelectButton.availableItems = smallMonthDaysArray;
            break;
        case 5:
            daySelectButton.availableItems = bigMonthDaysArray;
            break;
        case 6:
            daySelectButton.availableItems = smallMonthDaysArray;
            break;
        case 7:
            daySelectButton.availableItems = bigMonthDaysArray;
            break;
        case 8:
            daySelectButton.availableItems = bigMonthDaysArray;
            break;
        case 9:
            daySelectButton.availableItems = smallMonthDaysArray;
            break;
        case 10:
            daySelectButton.availableItems = bigMonthDaysArray;
            break;
        case 11:
            daySelectButton.availableItems = smallMonthDaysArray;
            break;
        case 12:
            daySelectButton.availableItems = bigMonthDaysArray;
            break;

        default:
            break;
    }
    
    
    for (NSItemObject *item in daySelectButton.availableItems){
        if (item.inner == daySelectButton.inner){
            return;
        }
    }
    //已经选择的日，超出了可选范围， 置为最后一个日子
    [daySelectButton setTitle:daySelectButton.availableItems.lastObject.str forState:UIControlStateNormal];
    daySelectButton.inner = daySelectButton.availableItems.lastObject.inner;
    return;
}
@end
